package com.fish.web.order;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fish.common.constant.Constant;
import com.fish.common.constant.QueryPage;
import com.fish.common.web.BaseAction;
import com.fish.order.dao.OrderDAO;
import com.fish.order.po.Order;
import com.fish.product.po.PerformTarget;
import com.fish.product.po.Product;
import com.fish.service.order.OrderService;
import com.fish.service.passport.MakePassCodeService;
import com.fish.service.product.ProductService;

public class OrderAction extends BaseAction {
	private static final long serialVersionUID = -955693477411885613L;
	private static final Log loger = LogFactory.getLog(OrderDAO.class);
	private OrderService orderService;
	private MakePassCodeService makePassCodeService;
	private ProductService productService;
	private Order order = new Order();
	private List<Order> orderList = new ArrayList<Order>();
	private String startDate;
	private String endDate;
	private QueryPage queryPage = new QueryPage();
	
	public String goToOrder(){
		 return SUCCESS;
	}

	public String goToOrderList() {
		if (startDate != null && !"".equals(startDate) && endDate != null && !"".equals(endDate)) {
			order.setStartDate(convertToDate(startDate));
			order.setEndDate(convertToDate(endDate));
		}
		if(!getSessionUser().isAdmin()){
			order.setUserId(getSessionUser().getUserId());
		}
		Long count = orderService.queryOrderCountByObject(order);
		if (count > 0) {
			queryPage = new QueryPage(count, Constant.DEFAULT_PAGE_SIZE, queryPage.getCurPage());
		}
		orderList = orderService.queryOrderByObject(order, queryPage);
		return SUCCESS;
	}

	public String viewOrderInfo(){
		if(order.getOrderId()!=null) {
			order = orderService.queryOrderByPrimaryKey(order.getOrderId());
		}
		 return SUCCESS;
	}
	
	public String saveOrder(){
		try {
			if(order.getOrderId()==null) {
				order.setOrderStatus("true");
				order.setCreateTime(new Date());
				order.setUpdateTime(new Date());
				order.setPerformStatus("false");
				order.setSettlementStatus("false");
				order.setOrderPrice(order.getQuantity()*order.getSellPrice());
				orderService.addOrder(order);
				//this.applayCode(order.getOrderId());
			} else {
				order.setUpdateTime(new Date());
				orderService.updateOrder(order);
			}
		} catch (Exception e) {
			loger.debug("保存订单有错！");
			loger.debug(e);
		}

		 return goToOrderList();
	}
	public String goToCreateOrder(){
		Product product =this.productService.queryProductByPrimaryKey(order.getProductId());
		this.order.setProductName(product.getProductName());
		this.order.setOrderType(product.getProductType());
		this.order.setSellPrice(product.getSellPrice());
		this.order.setSettlementPrice(product.getSettlementPrice());
		this.order.setUserId(product.getUserId());
		 return SUCCESS;
	}
	public void applayCode(){
		 Long orderId=this.getOrder().getOrderId();
		 Order order=this.orderService.queryOrderByPrimaryKey(orderId);
		 Map<Long,List<PerformTarget>>performTargetMap=new HashMap<Long,List<PerformTarget>>();
		 List<PerformTarget>performTargetList=new ArrayList<PerformTarget>();
		 PerformTarget performTarget=new PerformTarget();
		 performTarget.setTargetId("1");
		 performTarget.setTargetName("欢乐谷");
		 performTargetList.add(performTarget);
		 performTargetMap.put(orderId, performTargetList);
		 this.makePassCodeService.applyCodeForOrder(order, performTargetMap);
	}
	
	private Date convertToDate(String dateStr){
		try {
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			return format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Order getOrder() {
		return order;
	}
	
	public void setOrder(Order order) {
		this.order = order;
	}
	
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
	public List<Order> getOrderList() {
		return orderList;
	}
	public void setMakePassCodeService(MakePassCodeService makePassCodeService) {
		this.makePassCodeService = makePassCodeService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public QueryPage getQueryPage() {
		return queryPage;
	}

	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}

	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
	
	
}